#include <Constants.au3>
Dim $itunes = ObjCreate("iTunes.Application");

Opt("TrayAutoPause",   0);
Opt("TrayIconDebug",   0);
Opt("TrayMenuMode",    1);
Opt("TrayOnEventMode", 1);

HotKeySet("{MEDIA_PREV}",       "Back");
HotKeySet("{MEDIA_NEXT}",       "_Next");
HotKeySet("{MEDIA_PLAY_PAUSE}", "PlayPause");
HotKeySet("{MEDIA_STOP}",       "Stop");
HotKeySet("{VOLUME_UP}",        "VolUp");
HotKeySet("{VOLUME_DOWN}",      "VolDown");
HotKeySet("{VOLUME_MUTE}",      "VolMute");

TraySetToolTip("iHotKey");
TrayItemSetOnEvent(TrayCreateItem("&Exit"), "Close");

While True
	Sleep(200);
	If WinExists("iTunes", "One or more applications are using the iTunes scripting interface. Are you sure you want to quit?") Then
		
	EndIf
WEnd

Func Back()
	$itunes.BackTrack();
EndFunc

Func _Next()
	$itunes.NextTrack();
EndFunc

Func PlayPause()
	$itunes.PlayPause();
EndFunc

Func Stop()
	$itunes.Stop();
EndFunc

Func VolUp()
	Local $newvol = $itunes.SoundVolume + 10;
	If $newvol > 100 Then $newvol = 100;
	
	$itunes.SoundVolume = $newvol;
EndFunc

Func VolDown()
	Local $newvol = $itunes.SoundVolume - 10;
	If $newvol < 0 Then $newvol = 100;
	
	$itunes.SoundVolume = $newvol;
EndFunc

Func VolMute()
	If $itunes.SoundVolume <> 0 Then
		$itunes.SoundVolume = 0;
	Else
		$itunes.SoundVolume = 100;
	EndIf
EndFunc

Func Close()
	$itunes = 0;
	Exit(0);
EndFunc